<?php

/**
 * Created by PhpStorm.
 * User: henry
 * Date: 2017/3/2
 * Time: 上午11:13
 */

namespace common\encrypt;

class AESCryptography
{
    /**
     * 加密
     * @param $input
     * @param $key
     * @return string
     */
    public static function encrypt($input, $key)
    {
        return openssl_encrypt($input, 'AES-256-ECB', md5($key));
    }

    /**
     * 解密
     * @param $encryptStr
     * @param $key
     * @return string
     */
    public static function decrypt($encryptStr, $key)
    {
        $key = md5($key);
        $decrypted = openssl_decrypt(
            base64_decode($encryptStr),
            'AES-256-ECB',
            $key,
            OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING
        );
        $dec_s = strlen($decrypted);
        $padding = ord($decrypted[$dec_s - 1]);
        $decrypted = substr($decrypted, 0, -$padding);
        return $decrypted;
    }
}